﻿using MethodBoundaryAspect.Fody.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Marmat.Foundation;
using Marmat.FrameWork;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 权限注解，只有指定的用户才能访问该函数。
    /// </summary>
    public sealed class UserPermissionAttribute : OnMethodBoundaryAspect, Marmat.Template.WinForm.IPermissionAttribute, IProvider
    {
        internal readonly string[] userCollection;

        /// <summary>
        /// 初始化 Marmat.Template.WinForm.UserPermissionAttribute 类的新实例。
        /// </summary>
        /// <param name="users">传入的用户清单</param>
        public UserPermissionAttribute(params string[] users)
        {
            this.userCollection = users;
        }

        /// <summary>
        /// 进入方法前执行。
        /// </summary>
        /// <param name="arg"></param>
        public override void OnEntry(MethodExecutionArgs arg)
        {
            if (this.Verify())
                base.OnEntry(arg);
            else
                arg.FlowBehavior = FlowBehavior.Return;
        }

        /// <summary>
        /// 鉴权
        /// </summary>
        /// <returns></returns>
        public bool Verify()
        {
            var userProvider = SystemCommand.GetService<Marmat.Foundation.IUserProvider>();
            return (this.userCollection.Contains(userProvider.UserID));
        }
    }
}
